import logging
from itsdangerous import BadData
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

from apps.oauth.contants import OPENID_TOKEN_EXPIRES_TIME
from meiduo_mall import settings
def generic_openid_token(openid):

    # 创建实例对象
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=OPENID_TOKEN_EXPIRES_TIME)
    # 组织数据
    data = {
        'openid':openid
    }
    # 加密处理
    token = s.dumps(data)
    # 返回数据
    return token.decode()
logger =logging.getLogger('django')

def check_openid_token(token):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=OPENID_TOKEN_EXPIRES_TIME)
    try:
        result = s.loads(token)
    except BadData as e:
        logger.error(e)
        return None
    return result.get('openid')